package com.project.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsProductLabelBiz;
import com.project.entity.CmsProductLabel;
import com.project.mapper.CmsProductLabelMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 商品标签业务层接口类
 * @Author: zhuchi
 * @CreateDate: 2020/10/15 15:10
 * @Version: 1.0
 */
@Service
public class CmsProductLabelBizImpl extends ServiceImpl<CmsProductLabelMapper, CmsProductLabel> implements CmsProductLabelBiz {

    /**
     * 添加/修改商品标签
     * @param productLabel
     * @param productId
     * @param userId
     * @return
     */
    public int updateProductLabel(List<String> productLabel, Long productId, Long userId){
        //清空上面现有标签
        QueryWrapper<CmsProductLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        this.baseMapper.delete(queryWrapper);

        if (null != productLabel && productLabel.size() > 0){
            int addPorductLabel = this.baseMapper.insertProductLabel(productLabel, productId, userId);
            return addPorductLabel;
        }
        return 0;
    }

    /**
     * 获取商品标签
     * @param productId
     * @return
     */
    public List<String> getProductLabel(Long productId){
        QueryWrapper<CmsProductLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        List<CmsProductLabel> list = this.baseMapper.selectList(queryWrapper);

        List<String> labels = new ArrayList<>();
        if (null != list && list.size() > 0){
            for (CmsProductLabel productLabel : list){
                labels.add(productLabel.getLabelName());
            }
        }

        return labels;
    }

}
